(() => {
    let ele = {};

    ele.options = $('#tab-team li');
    ele.options2 = $('#tab-two button');

    ele.list_one = $('#list-one')//瓜分灵石
    ele.list_five = $('#list-five')//天神榜

    ele.list_all = $('#list-all')//全服首杀
    ele.list_part = $('#list-part')//全服首杀

    ele.xianmeng = $('#xianmeng')//仙盟名称
    ele.jueseming = $('#jueseming')//盟主角色名

    ele.guowang = $('#guowang')//国王
    ele.wanghou = $('#wanghou')//王后

    ele.list_six = $('#list-six')//推广榜

    ele.qufuName = $('.qufuName')
    for (let i = 0; i < ele.qufuName.length; i++) {
        ele.qufuName[i].innerHTML = getCookie('server_name') || '暂未绑定'
    }

    let typeName = 'one';
    let typeName2 = 'aa';
    let currentPage = {
        one: 1,
        two: 2,
        three: 3,
        four: 4,
        five: 5,
        six: 6,
        seven: 7
    }

    // 瓜分灵石的数据
    const Carveup = [
        {
            level: '280-299级',
            linshi: '4000'
        },
        {
            level: '310-319级',
            linshi: '3000'
        },
        {
            level: '320级以上',
            linshi: '3000'
        },
    ]

    function CarveupData() {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.CARVEUPRANK,
            method: 'GET',
            header: {
                Authorization: token
            },
            success: res => {
                if (res.code == 1) CarveupList(Carveup, res.data);
                else openTips(res.msg);
            }
        });
    }

    CarveupData()
    tabBar();
    // 选项卡选项功能
    function tabBar() {
        for (let i = 0; i < ele.options.length; i++) {
            ele.options[i].addEventListener('click', e => {
                let cls = e.target.className;
                typeName = cls.split(' ')[0].split('-')[1];
                console.log(currentPage[typeName]);
                if (currentPage[typeName] == 1) {
                    CarveupData()
                } else if (currentPage[typeName] == 2) {
                } else if (currentPage[typeName] == 3) {
                    shendian()
                } else if (currentPage[typeName] == 4) {
                    getList();//世界领主
                    getList2()//领主之家
                } else if (currentPage[typeName] == 5) {
                    gettianshen()//天神榜
                } else if (currentPage[typeName] == 6) {
                    tuiguang()
                } else if (currentPage[typeName] == 7) {
                    gettseven()
                }

            })
        }
    }



    // 瓜分灵石渲染
    function CarveupList(data, Tdata) {
        let html = '';
        for (let i = 0; i < data.length; i++) {
            let d = data[i];
            html += '<div class="link">' +
                '<p class="rp">开服10天内达到' + d.level + '的玩家</p>' +
                '<div class="roab">' +
                '<div class="reward">' +
                '<img class="linshi" src="http://img.qunx.top/xianwang/public/lin.png" alt="">' +
                '<span class="rnum">瓜分' + d.linshi + '灵石</span>' +
                '</div>' +
                '<p class="rp2">当前达成人数:' + Tdata[i] + '</p>' +
                '</div>' +
                '</div>'
        }
        ele.list_one.innerHTML = html;

    }
    const typeList = ['', 'aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg'];
    getFriendList(typeList.indexOf(typeName2));
    // 天骄榜
    tabBar2()
    function tabBar2() {
        for (let i = 0; i < ele.options2.length; i++) {
            ele.options2[i].addEventListener('click', e => {
                let cls = e.target.className;
                typeName2 = cls.split(' ')[0].split('-')[1];
                let typeIndex = typeList.indexOf(typeName2);
                if (typeName2 == 'aa') {
                    getFriendList(typeIndex);
                } else if (typeName2 == 'bb') {
                    getFriendList(typeIndex);
                } else if (typeName2 == 'cc') {
                    getFriendList(typeIndex);
                } else if (typeName2 == 'dd') {
                    getFriendList(typeIndex);
                } else if (typeName2 == 'ee') {
                    getFriendList(typeIndex);
                } else if (typeName2 == 'ff') {
                    getFriendList(typeIndex);
                } else if (typeName2 == 'gg') {
                    getFriendList(typeIndex);
                }
            })
        }
    }
    // 拉取天骄榜列表
    function getFriendList(type) {
        // console.log(type, typeName2);
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.TIANJIAOBANGLIST,
            method: 'GET',
            header: {
                Authorization: token
            },
            data: {
                type: type
            },
            success: res => {
                if (res.code == 1) {
                    drawFriendList(res.data.data);
                } else openTips(res.msg);
            }
        });
    }
    // 渲染天骄榜列表
    function drawFriendList(data) {
        let elm = $('#list-' + typeName2);
        let html = '';
        for (let i = 0; i < data.length; i++) {
            let t = data[i]
            let name = t.role_name ? t.role_name : '待公布'
            let num = ''
            if (t.sort == 1) {
                num = '500'
            } else if (t.sort == 2 || t.sort == 3) {
                num = '200'
            } else {
                num = '50'
            }
            html += '<div class="lisk">' +
                '<div class="lie">' +
                '<p class="ls1">第' + t.sort + '名</p>' +
                '<p class="ls2">上榜者：' + name + '</p>' +
                '</div>' +
                '<div class="reward">' +
                '<img class="linshi" src="http://img.qunx.top/xianwang/public/lin.png" alt="">' +
                '<span class="rnum">奖励' + num + '灵石</span>' +
                '</div>' +
                '</div>'
        }
        elm.innerHTML = html;

    }

    // 神殿争霸
    function shendian() {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.SHENDIANZHENGBA,
            method: 'GET',
            header: {
                Authorization: token
            },
            success: res => {
                if (res.code == 1) {
                    ele.jueseming.innerHTML = res.data[0].role_name || '待公布'
                    ele.xianmeng.innerHTML = res.data[0].title || '待公布'
                }
                else openTips(res.msg);
            }
        });
    }

    // 获取全服首杀 世界领主列表
    function getList() {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.QUANFUSHOUSHA,
            method: 'GET',
            header: {
                Authorization: token
            },
            data: {
                type: 8
            },
            success: res => {
                if (res.code == 1) drawPropList(res.data);
                else openTips(res.msg);
            }
        });
    }

    // 渲染全服首杀世界领主列表
    function drawPropList(data) {
        let html = '';
        for (let i = 0; i < data.length; i++) {
            let lv = data[i];
            let name = lv.role_name ? lv.role_name : '暂无归属'
            let num = 30
            // if (lv.sort == 1) {
            //     num = '500'
            // } else if (lv.sort == 2 || lv.sort == 3) {
            //     num = '200'
            // } else {
            //     num = '50'
            // }
            html += '<div class="lisk">' +
                '<div class="lie">' +
                '<p class="ls1">' + lv.title + '</p>' +
                '<p class="ls2">上榜者：' + name + '</p>' +
                '</div>' +
                '<div class="reward">' +
                '<img class="linshi" src="http://img.qunx.top/xianwang/public/lin.png" alt="">' +
                '<span class="rnum">奖励' + num + '灵石</span>' +
                '</div>' +
                '</div>'

        }
        ele.list_all.innerHTML = html;
    }




    // 获取全服首杀领主之家列表
    function getList2() {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.QUANFUSHOUSHA,
            method: 'GET',
            header: {
                Authorization: token
            },
            data: {
                type: 9
            },
            success: res => {
                if (res.code == 1) drawPropList2(res.data);
                else openTips(res.msg);
            }
        });
    }

    // 渲染全服首杀列表
    function drawPropList2(data) {
        let html = '';
        for (let i = 0; i < data.length; i++) {
            let lv = data[i];
            let name = lv.role_name ? lv.role_name : '暂无归属'
            let num = 88

            html += '<div class="lisk">' +
                '<div class="lie">' +
                '<p class="ls1">' + lv.title + '</p>' +
                '<p class="ls2">上榜者：' + name + '</p>' +
                '</div>' +
                '<div class="reward">' +
                '<img class="linshi" src="http://img.qunx.top/xianwang/public/lin.png" alt="">' +
                '<span class="rnum">奖励' + num + '灵石</span>' +
                '</div>' +
                '</div>'

        }
        ele.list_part.innerHTML = html;
    }



    // 推广榜
    function tuiguang() {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.PROMOTIN,
            method: 'GET',
            header: {
                Authorization: token
            },
            success: res => {
                if (res.code == 1) drawPropList3(res.data);
                else openTips(res.msg);
            }
        });
    }

    // 渲染推广榜列表
    function drawPropList3(data) {
        let html = '';
        for (let i = 0; i < data.length; i++) {
            let lv = data[i];
            let name = lv.agent ? lv.agent.nick_name : '待公布'
            let num = ''
            if (lv.sort == 1) {
                num = '500'
            } else if (lv.sort == 2) {
                num = '300'
            } else if (lv.sort == 3) {
                num = '200'
            }
            else {
                num = '100'
            }
            html += '<div class="link">' +
                '<p class="rp">第' + lv.sort + '名(要求: 至少直推' + num + '个仙王)</p>' +
                '<div class="reward">' +
                '<img class="linshi" src="http://img.qunx.top/xianwang/public/hongbao.png" alt="">' +
                '<span class="rnum">奖励' + lv.money + '零钱</span>' +
                '</div>' +
                '<p class="ls3">上榜者：' + name + '</p>' +
                '</div>'

        }
        ele.list_six.innerHTML = html;
    }


    // 天下三分
    function gettseven() {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.TIANXIASANFEN,
            method: 'GET',
            header: {
                Authorization: token
            },
            success: res => {
                if (res.code == 1) {
                    ele.guowang.innerHTML = res.data[0].role_name || '待公布'
                    ele.wanghou.innerHTML = res.data[0].title || '待公布'
                }
                else openTips(res.msg);
            }
        });
    }


    // 天神榜
    function gettianshen() {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.TIANXIASANFENLIST,
            method: 'GET',
            header: {
                Authorization: token
            },
            data: {
                type: 16
            },
            success: res => {
                if (res.code == 1) {
                    drawtian(res.data.data);
                } else openTips(res.msg);
            }
        });
    }
    // 渲染天神榜列表
    function drawtian(data) {
        let html = '';
        for (let i = 0; i < data.length; i++) {
            let t = data[i]
            let name = t.role_name ? t.role_name : '待公布'
            let num = ''
            if (t.sort == 1) {
                num = '300'
            } else {
                num = '50'
            }
            html += '<div class="lisk">' +
                '<div class="lie">' +
                '<p class="ls1">第' + t.sort + '名</p>' +
                '<p class="ls2">上榜者：' + name + '</p>' +
                '</div>' +
                '<div class="reward">' +
                '<img class="linshi" src="http://img.qunx.top/xianwang/public/lin.png" alt="">' +
                '<span class="rnum">奖励' + num + '灵石</span>' +
                '</div>' +
                '</div>'
        }
        ele.list_five.innerHTML = html;

    }

})();